#
# @lc app=leetcode.cn id=112 lang=python3
#
# [112] 路径总和
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    isEqual = False
    sumTree = 0
    def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:
                
        if root == None: return False
        
        Solution.isEqual = False
        Solution.sumTree = 0
        self.midOrder(root,targetSum)
        return Solution.isEqual
        # def midOrder(treeNode):
        #     if isEqual: return
        #     valNum = treeNode.val
        #     sumTree += valNum

        #     if treeNode.left == None and treeNode.right == None and sumTree == targetSum:
        #         isEqual = True
        #         return
            
        #     if treeNode.left != None:
        #         midOrder(treeNode.left)     
        #     if treeNode.right != None:
        #         midOrder(treeNode.right)

        #     sumTree -= valNum
        #     return
        
        # sumTree = [0, False]
        # if root == None: return False
        # midOrder(root)
        # return isEqual
        
        #使用静态变量

    def midOrder(self, treeNode, targetSum):
        if Solution.isEqual: return
        valNum = treeNode.val
        Solution.sumTree += valNum

        if treeNode.left == None and treeNode.right == None and Solution.sumTree == targetSum:
            Solution.isEqual = True
            return
        
        if treeNode.left != None:
            self.midOrder(treeNode.left, targetSum)     
        if treeNode.right != None:
            self.midOrder(treeNode.right, targetSum)

        Solution.sumTree -= valNum
        return

        
# @lc code=end

